package com.atguigu.srb.core.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.context.annotation.Configuration;

import java.util.Date;

/*
mybatisplus对时间日期的自动填充
 */
@Configuration
public class GlobalMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        if(metaObject.hasSetter("createTime") &&
                this.getFieldValByName("createTime",metaObject)==null){
            metaObject.setValue("createTime",new Date());
        }
        if(metaObject.hasSetter("updateTime") &&
                this.getFieldValByName("updateTime",metaObject)==null){
            metaObject.setValue("updateTime",new Date());
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        if(metaObject.hasSetter("updateTime") &&
                this.getFieldValByName("updateTime",metaObject)==null){
            metaObject.setValue("updateTime",new Date());
        }
    }
}
